import 'package:flutter/material.dart';
import 'package:flutter_base/provider_demo3/models/task.dart';
import 'package:provider/provider.dart';

class SecondPage extends StatefulWidget {
  @override
  State<SecondPage> createState() => _SecondPageState();
}

class _SecondPageState extends State<SecondPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('二级页面')),
      body: Center(
        child: Selector<Task, TaskStats>(
          selector: (_, task) => task.stats,
          builder: (_, stats, __) => Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Card(
                margin: const EdgeInsets.all(10.0),
                child: Column(children: [
                  Text('total: ${stats.total}', style: const TextStyle(fontSize: 18.0)),
                  Text('completion: ${stats.completion}', style: const TextStyle(fontSize: 18.0)),
                ]),
              ),
              ElevatedButton(
                onPressed: () {
                  Provider.of<Task>(context, listen: false).increseCompletion();
                },
                child: const Text('更新统计'),
              ),
            ],
          ),
        ),
        /*
        child: ElevatedButton(
          onPressed: () {
            Provider.of<Task>(context, listen: false).increseCompletion();
          },
          child: const Text('更新统计'),
        ),
        */
      ),
    );
  }
}
